package com.whisperarts.financer.app.model.rcp;

import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import com.whisperarts.financer.model.account.Account;
import com.whisperarts.financer.model.account.AccountsCategory;

public class AccountsLabelProvider extends StyledCellLabelProvider {
	/*
	@Override
	public String getText(Object element) {
		if (element instanceof AccountsCategory) {
			return ((AccountsCategory) element).getName();
		}
		return ((Account) element).getTitle();
	}
	
	@Override
	public Image getImage(Object element) {
		if (element instanceof AccountsCategory) {
			return PlatformUI.getWorkbench().getSharedImages()
				.getImage(ISharedImages.IMG_OBJ_FOLDER);
		}
		return PlatformUI.getWorkbench().getSharedImages()
			.getImage(ISharedImages.IMG_OBJ_FILE);
	}
	*/
	
	@Override
	public void update(ViewerCell cell) {
		Object element = cell.getElement();
		StyledString text = new StyledString();
		
		if (element instanceof AccountsCategory) {
			AccountsCategory category = (AccountsCategory) element;
			text.append(category.getName());
			cell.setImage(PlatformUI.getWorkbench().getSharedImages()
					.getImage(ISharedImages.IMG_OBJ_FOLDER));
			text.append(" (" + category.getAccounts().size() + ") ", StyledString.COUNTER_STYLER);
		} else if (element instanceof Account) {
			Account account = (Account) element;
			text.append(account.getName());
			cell.setImage(PlatformUI.getWorkbench().getSharedImages()
					.getImage(ISharedImages.IMG_OBJ_FILE));
		} else {			
			text.append(element.toString(), StyledString.DECORATIONS_STYLER);			
			cell.setImage(null);
			
		}
		cell.setText(text.toString());
		cell.setStyleRanges(text.getStyleRanges());
		
		super.update(cell);
	}
}
